package com.chao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chao.entity.Major;
import com.chao.entity.Msg;
import com.chao.entity.Student;
import com.chao.service.impl.MajorServiceImpl;
import com.chao.utils.TypeConversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.management.Query;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Controller
public class MajorController {

    @Autowired
    MajorServiceImpl majorService;


    // 条件查询
    @GetMapping("/major_query")
    @ResponseBody
    public Msg major_all(Major major) {
        QueryWrapper<Major> wrapper = new QueryWrapper<>();
        wrapper.eq("m_department", major.getMDepartment());
        List<Major> majors = majorService.list(wrapper);
        return Msg.success().add("majors", majors);
    }


    // 更新数据
    @PutMapping("/major/{mId}")
    @ResponseBody
    public Msg major_update(@PathVariable Integer mId,
                            @Valid Major major) {
        major.setMId(mId);
        major.setMDate(new Date());
        majorService.updateById(major);
        return Msg.success();
    }


    // 批量删除 和 单个删除一起
    @DeleteMapping("/major/{mIds}")
    @ResponseBody
    public Msg major_delete(@PathVariable String mIds) {
        if (mIds.contains("-")) {
            String[] strings = mIds.split("-");
            List<Integer> integerList = TypeConversion.strArrToIntegerList(strings);
            majorService.removeBatchByIds(integerList);
        } else {
            majorService.removeById(Integer.parseInt(mIds));
        }
        return Msg.success();
    }

    @PostMapping("/major")
    @ResponseBody
    public Msg major_add(@Valid Major major) {
        major.setMDate(new Date());
        majorService.addOfPrimary(major);
        return Msg.success();
    }

    @GetMapping("/major")
    @ResponseBody
    public Msg major(@RequestParam(defaultValue = "1") Integer pn) {
        Page<Major> page = new Page<>(pn, 5);
        Page<Major> majorPage = majorService.selectAll(page);
        List<Major> majors = majorPage.getRecords();
        return Msg.success().add("page", page).add("majors", majors);
    }

    // 检查 专业 是否已存在
    @GetMapping("/checkMajor")
    @ResponseBody
    public Msg checkMajor(@RequestParam String mName) {
        QueryWrapper<Major> wrapper = new QueryWrapper<>();
        wrapper.eq("m_name", mName);
        boolean exists = majorService.getBaseMapper().exists(wrapper);
        if (exists) {
            return Msg.fail().add("msg", "该专业已经存在");
        } else {
            return Msg.success();
        }

    }

    @GetMapping("/major_table")
    public String major_table() {
        return "major/major_table";
    }
}
